package com.xilihui.xlh.business.activitys.store;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.EditText;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.requests.StoreRequest;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.app.BaseEntity;
import com.xilihui.xlh.core.app.ToolBaseCompatActivity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import org.feezu.liuli.timeselector.Utils.TextUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/9/7.
 */

public class PostEvaluationActivity extends ToolBaseCompatActivity{
    
    @BindView(R.id.sdv)
    SimpleDraweeView sdv;
    @BindView(R.id.tv_hao)
    TextView tv_hao;
    @BindView(R.id.tv_zhong)
    TextView tv_zhong;
    @BindView(R.id.tv_cha)
    TextView tv_cha;
    @BindView(R.id.et_evaluation)
    EditText et_evaluation;        
            
    String order_id = "";
    String image = "";
    int zan_num = -1;
    
    @OnClick(R.id.iv_toolbar_left)
    public void back(){
        finish();
    }
    
    @Override
    public int getLayout() {
        return R.layout.activity_post_evaluation;
    }

    @Override
    public String initTitle() {
        return null;
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        setToolTitle("发表评价");
        setToolRightText("发布");
        
        order_id = getIntent().getStringExtra("order_id");
        image = getIntent().getStringExtra("image");
        sdv.setImageURI(UrlConst.baseUrl()+image);
        
    }

    @Override
    public void initData() {
        
    }
    
    
    @OnClick(R.id.tv_hao)
    public void hao(){
        tv_hao.setSelected(true);
        tv_zhong.setSelected(false);
        tv_cha.setSelected(false);
        zan_num = 2;
    }
    
    @OnClick(R.id.tv_zhong)
    public void zhong(){
        tv_hao.setSelected(false);
        tv_zhong.setSelected(true);
        tv_cha.setSelected(false);
        zan_num = 1;
    }
    
    @OnClick(R.id.tv_cha)
    public void cha(){
        tv_hao.setSelected(false);
        tv_zhong.setSelected(false);
        tv_cha.setSelected(true);
        zan_num = 0;
    }
    
    /**
     * 发布评论
     */
    @OnClick(R.id.tv_toolbar_right)
    public void postEvaluation(){
        
        if(zan_num==-1){
            ToastUtil.toastShortNegative("请选择评价等级");
            return;
        }
        
        String content = et_evaluation.getText().toString();
        if(TextUtil.isEmpty(content)){
            ToastUtil.toastShortNegative("请输入评价内容");
            return;
        }
        
        StoreRequest.postEvaluation(this,order_id,content,zan_num+"")
                .compose(DoTransform.<BaseEntity>applyScheduler(PostEvaluationActivity.this,true))
                .compose(this.<BaseEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<BaseEntity>(PostEvaluationActivity.this) {
                    @Override
                    protected void onSuccess(BaseEntity entity) {
                        ToastUtil.toastShortPositive(entity.getMsg());
                        YEventBuses.post(new YEventBuses.Event("yipingjia"));
                        finish();
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });
    }
    
}
